package info.webest.reminder;

// http://habrahabr.ru/post/114515/
// http://stackoverflow.com/questions/16903344/updating-widget-using-android-service
// https://code.google.com/p/mytracks/source/browse/MyTracks/src/com/google/android/apps/mytracks/widgets/AppWidgetProvider.java?name=v1.1.7&r=ed80a23683a85cce3d5853150054107f3e6d3095
// http://stackoverflow.com/questions/11237971/how-to-observe-contentprovider-change-android
// http://stackoverflow.com/questions/25686018/android-how-to-detect-contact-list-is-changed?rq=1

import info.webest.reminder.service.UpdateService;

import android.annotation.TargetApi;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.database.ContentObserver;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.provider.ContactsContract;
import android.util.Log;
import android.widget.RemoteViews;
import android.widget.Toast;

public class WidgetProvider extends AppWidgetProvider {

	private static Context mContext = null;

	public static final ContentObserver observer = new ContentObserver(
			new Handler()) {

		@Override
		public void onChange(boolean selfChange) {
			super.onChange(selfChange);

			Toast.makeText(mContext,
					"Webest Reminder: contact list was changed",
					Toast.LENGTH_SHORT).show();

			if (mContext != null)
				update(mContext);
		}
	};
	
	
	@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
	@Override
	public void onAppWidgetOptionsChanged(Context context,
			AppWidgetManager appWidgetManager, int appWidgetId, Bundle bundle) {

		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
			// int minWidth = bundle
			// .getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH);

			// int maxWidth = bundle
			// .getInt(AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH);

			// int minHeight = bundle
			// .getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT);

			// int maxHeight = bundle
			// .getInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT);

			Configuration config = context.getResources().getConfiguration();
			int orientation = config.orientation;

			switch (orientation) {

			case Configuration.ORIENTATION_PORTRAIT:
				UpdateService.ItemCount = 5;
				break;

			case Configuration.ORIENTATION_LANDSCAPE:
				UpdateService.ItemCount = 10;
				break;

			default:
				break;
			}
			RemoteViews rv = null;
			rv = new RemoteViews(context.getPackageName(), R.layout.widget_1x4);
			appWidgetManager.updateAppWidget(appWidgetId, rv);
		}

		// RemoteViews rv = null;
		// Size of a 2x1 widget on a Nexus 4 (other screen densities will
		// differ)
		// if (minWidth == 152 && maxWidth == 196 && minHeight == 58
		// && maxHeight == 84) {
		// rv = new RemoteViews(context.getPackageName(), R.layout.widget2x1);
		// } else {
		// rv = new RemoteViews(context.getPackageName(), R.layout.widget);
		// }
		// appWidgetManager.updateAppWidget(appWidgetId, rv);
	}

	@Override
	public void onEnabled(Context context) {
		Log.e("onEnabled", "@@@ ");
		mContext = context;

		context.getContentResolver().registerContentObserver(
				ContactsContract.Contacts.CONTENT_URI, true, observer);
	}

	@Override
	public void onDisabled(Context context) {
		Log.e("onDisabled", "@@@ ");
		mContext = context;
		context.getContentResolver().unregisterContentObserver(observer);
	}

	// @Override
	// public void onReceive(Context context, Intent intent) {
	// Log.e("onReceive", "@@@ ");
	// super.onReceive(context, intent);
	// update(context);
	// }

	public static void update(Context context) {
		Log.i("update method", "Called");

		// update widget via service
		context.startService(new Intent(context, UpdateService.class));
	}

	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager,
			int[] appWidgetIds) {

		mContext = context;

		// To prevent any ANR timeouts, we perform the update in a service
		update(context);
	}

}